package app.mvp.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;

/**
 * 轻量级数据存储工具
 *
 * @author Created by Cao.yh on 2015-9-30
 */
public class SharedPreferencesUtil {

    /**
     * 存入
     *
     * @param context
     * @param key
     * @param value
     */
    public static void put(Context context, String key, String value) {
        SharedPreferences preferences = PreferenceManager
                .getDefaultSharedPreferences(context);
        Editor editor = preferences.edit();
        editor.putString(key, value);
        editor.commit();
    }

    /**
     * 取出
     *
     * @param context
     * @param key
     * @param defValue
     * @return
     */
    public static String get(Context context, String key, String defValue) {
        SharedPreferences preferences = PreferenceManager
                .getDefaultSharedPreferences(context);
        return preferences.getString(key, defValue);
    }

    public static void put(Context context, String key, boolean value) {
        SharedPreferences preferences = PreferenceManager
                .getDefaultSharedPreferences(context);
        Editor editor = preferences.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    public static boolean get(Context context, String key, boolean defValue) {
        SharedPreferences preferences = PreferenceManager
                .getDefaultSharedPreferences(context);
        return preferences.getBoolean(key, defValue);
    }

    public static void clear(Context context, String key) {
        SharedPreferences preferences = PreferenceManager
                .getDefaultSharedPreferences(context);
        preferences.edit().remove(key).commit();
    }

}
